package com.powerbosframework.web.form;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.powerbosframework.beans.access.BeanFactory;
import com.powerbosframework.beans.access.Setter;
import com.powerbosframework.util.TypeConvertUtil;
import com.powerbosframework.web.action.SupportAction;
import com.powerbosframework.web.util.RequestURI;

public class FormBeanHelp {
	private HttpServletRequest request;	
	private SupportAction supportAction;
	private String method="post";
	private Object entityObj=null;
	private String entityName="";
	
	public FormBeanHelp(){
		
	}
	public FormBeanHelp(HttpServletRequest request,SupportAction supportAction){
		this.request=request;
		this.supportAction=supportAction;
		method = this.request.getMethod().toLowerCase();		
	}
	//判断是否POJO
	public boolean isEntityBean(String name){
		String beanPro="";
		if(name.indexOf(".")!=-1){
			beanPro=name.substring(0, name.indexOf("."));			
			try {
				Field field=supportAction.getClass().getDeclaredField(beanPro);
				if(field==null) return false;
			} catch (SecurityException e) {				
				e.printStackTrace();
			} catch (NoSuchFieldException e) {				
				e.printStackTrace();
			}
			return true;
		}else{
			return false;
		}		
	}
	public void createFormBean(FormProperty formProperty) throws InstantiationException, IllegalAccessException{			
		if(entityObj==null){				
			String name=formProperty.getName().substring(0,formProperty.getName().indexOf("."));
			Field field=null;
			try {
				field = supportAction.getClass().getDeclaredField(name);
			} catch (NoSuchFieldException e) {
				if(field==null){			
					return;
				}
			}
			entityObj=field.getType().newInstance();	
			entityName=name;
		}					
		formProperty.setName(formProperty.getName().substring(formProperty.getName().indexOf(".")+1,formProperty.getName().length()));
		this.setProperty(entityObj,formProperty);		
	}
	public void setFormBean(){		
		Setter setter = BeanFactory.getSetter(supportAction.getClass(),entityName);
		setter.set(supportAction,entityObj);
		entityObj=null;	
	}
	public void setProperty(Object obj,FormProperty formProperty) throws SecurityException{	
		String name=formProperty.getName();		
		Object value=formProperty.getValue();
		Field field=null;
		try {
			field = obj.getClass().getDeclaredField(name);
		} catch (NoSuchFieldException e) {
			if(field==null){			
				return;
			}
		}		
		String type=field.getType().getSimpleName();		
		if(this.isPrimitive(type)){
			if("post".equals(method)){
				formProperty.setValue(TypeConvertUtil.convert(RequestURI.getPostEncode(value.toString()), type));	
			}else{
				formProperty.setValue(TypeConvertUtil.convert(value, type));	
			}
					
		}else if(type.equals("String[]")){
			
		}
		try {
			field.setAccessible(true);
			field.set(obj, formProperty.getValue());
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		//Setter setter = BeanFactory.getSetter(obj.getClass(),name);	
		//setter.set(obj,formProperty.getValue());
	}
	private boolean isPrimitive(String type){
		if(type==null) return false;
		if(type.equals("int")){
			return true;
		}else if(type.equals("Integer")){
			return true;
		}if(type.equals("String")){
			return true;
		}else if(type.equals("long")){
			return true;
		}else if(type.equals("Long")){
			return true;
		}else if(type.equals("Date")){
			return true;
		}
		return false;
	}
}
